package com.losdun.aftersales.controller;

import com.losdun.aftersales.domain.Repair;
import com.losdun.aftersales.manager.NoticeManager;
import com.losdun.aftersales.service.IRepairService;
import com.losdun.common.annotation.Log;
import com.losdun.common.core.controller.BaseController;
import com.losdun.common.core.domain.AjaxResult;
import com.losdun.common.core.page.TableDataInfo;
import com.losdun.common.enums.BusinessType;
import com.losdun.common.utils.poi.ExcelUtil;
import com.losdun.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 售后维保Controller
 * 
 * @author swordye
 * @date 2025-08-19
 */
@RestController
@RequestMapping("/aftersales/repair")
@Slf4j
public class RepairController extends BaseController
{
    @Autowired
    private IRepairService repairService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private NoticeManager noticeManager;

    /**
     * 查询售后维保列表
     */
    @PreAuthorize("@ss.hasPermi('aftersales:repair:list')")
    @GetMapping("/list")
    public TableDataInfo list(Repair repair)
    {
        startPage();
        List<Repair> list = repairService.selectRepairList(repair);
        list.forEach(item ->{
            if(item.getWorkerId() != null){
                item.setWorker(sysUserService.selectUserById(item.getWorkerId()));
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出售后维保列表
     */
    @PreAuthorize("@ss.hasPermi('aftersales:repair:export')")
    @Log(title = "售后维保", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Repair repair)
    {
        List<Repair> list = repairService.selectRepairList(repair);
        ExcelUtil<Repair> util = new ExcelUtil<Repair>(Repair.class);
        util.exportExcel(response, list, "售后维保数据");
    }

    /**
     * 获取售后维保详细信息
     */
    @PreAuthorize("@ss.hasPermi('aftersales:repair:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(repairService.selectRepairById(id));
    }

    /**
     * 新增售后维保
     */
    @PreAuthorize("@ss.hasPermi('aftersales:repair:add')")
    @Log(title = "售后维保", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Repair repair)
    {
        return toAjax(repairService.insertRepair(repair));
    }

    @PreAuthorize("@ss.hasPermi('aftersales:repair:add')")
    @PostMapping( "/agentAdd")
    public AjaxResult agentAdd(@RequestBody Repair repair) {
        try {
            repairService.appInsertRepair(repair);
            return success();
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            return error(e.getMessage());
        }
    }

    /**
     * 修改售后维保
     */
    @PreAuthorize("@ss.hasPermi('aftersales:repair:edit')")
    @Log(title = "售后维保", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Repair repair)
    {
        boolean isChangeWorker;
        try {
            isChangeWorker=repairService.checkChangeWorker(repair);
            int ret=repairService.updateRepair(repair);
            if(isChangeWorker){
                noticeManager.repairAssignWorkerSendNotice(repair);
            }
            return toAjax(ret);
        } catch (Exception e) {
            log.error("修改安装工单失败",e.getMessage(),e);
            return error(e.getMessage());
        }
    }

    /**
     * 删除售后维保
     */
    @PreAuthorize("@ss.hasPermi('aftersales:repair:remove')")
    @Log(title = "售后维保", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(repairService.deleteRepairByIds(ids));
    }
}
